package by.gamedev.site.client.app.ui;

import java.text.ParseException;

import com.google.gwt.text.shared.Parser;

/**
 * Simple parser of Character.
 */
public class CharParser implements Parser<Character> {

    private static CharParser INSTANCE;

    /**
     * @return the instance of the no-op renderer
     */
    public static Parser<Character> instance() {
        if(INSTANCE == null ) {
            INSTANCE = new CharParser();
        }
        return INSTANCE;
    }

    protected CharParser() {
        // nothing
    }

    @Override
    public Character parse(final CharSequence object) throws ParseException {
        if(object == null || object.length() == 0 || "".equals(object.toString()) ) {
            return null;
        }
        try {
            return object.charAt(0);
        } catch (final IndexOutOfBoundsException e) {
            throw new ParseException(e.getMessage(), 0);
        }
    }
}
